import { 
  CHANGE_THEME,
  OPEN_LAYOUTS_DIALOG,
  CLOSE_LAYOUTS_DIALOG,
  OPEN_LAYOUTS_SNACKBAR,
  CLOSE_LAYOUTS_SNACKBAR,
  TRIGGER_LAYOUTS_DRAWER,
  USER_PROFILE,
  GET_INIT_NOTIFICATION,
  CLEAR_NOTIFICATION,
  ADD_NOTIFICATION,
  WINDOW_WIDTH_CHANGE,
} from '../constants';

import CONFIG from '../../config';
import { parseJSON } from '../Helper';
import { replace, push, goBack } from 'react-router-redux';
import { logoutAndRedirect, loginUserFailure } from './LoginAc';

/**
 * 跳转页面replace方法
 */
export function replaceTo(path) {
  return (dispatch) => {
    dispatch(replace(path));
  }
}

/**
 * 跳转页面push方法
 */
export function pushTo(path) {
  return (dispatch) => {
    dispatch(push(path));
  }
}

/**
 * 跳转页面goBack方法
 */
export function backTo() {
  return (dispatch) => {
    dispatch(goBack());
  }
}

/**
 * 退出登录
 */
export function logout(location = '/') {
  return (dispatch) => {
    dispatch(logoutAndRedirect(location));
  }
}

/**
 * token过期提示
 */
export function tokenExpired() {
  let error = {msg: '账户已过期请重新登录'};
  return (dispatch) => {
    dispatch(logout('/login'));
    dispatch(loginUserFailure(error));
  }
}

/**
 * 打开会话框
 */
export function handleOpen(name) {
  return { 
    type: OPEN_LAYOUTS_DIALOG,
    payload: name
  }
}

/**
 * 关闭会话框
 */
export function handleClose(name) {
  return { 
    type: CLOSE_LAYOUTS_DIALOG,
    payload: name
  }
}

/**
 * 开启Snackbar
 */
export function handleOpenSnackbar(message) {
  return { 
    type: OPEN_LAYOUTS_SNACKBAR,
    payload: message
  }
}

/**
 * 关闭Snackbar
 */
export function handleCloseSnackbar() {
  return { 
    type: CLOSE_LAYOUTS_SNACKBAR
  }
}

/**
 * 点击触发打开和关闭Drawer
 */
export function handleTriggerDrawer() {
  return { 
    type: TRIGGER_LAYOUTS_DRAWER
  }
}

/**
 * 修改主题
 */
// 根据URL修改主题色彩
// 打开或者关闭tabs
export function changeTheme(url) {
  return { 
    type: CHANGE_THEME,
    payload: url
  }
}

/**
 * 可视窗口大小发生变化
 */
export function windowWidthChange(status) {
  // console.log(status)
  return {
    type: WINDOW_WIDTH_CHANGE,
    payload: status
  }
}

/**
 * 获取用户资料
 */
// 数据写入state
export function get(data) {
  return {
    type: USER_PROFILE,
    payload: data
  }
}

/**
 * 获取用户个人资料并且查看token状态
 */
export function getData(id, token) {
  return (dispatch) => {
    return fetch(CONFIG.apiHost + '/api/user/' + id + '?token=' + token, {
      method: 'get',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
    })
    .then(parseJSON)
    .then(res => {
      if (res.status) {
        if (res.msg.is_new_user) {
          dispatch(get(res.msg));
          dispatch(push('/create/profile'))
        }
        dispatch(get(res.msg));
      }
      if (res.error) {
        dispatch(tokenExpired());
      }
    })
  }
}

/**
 * 通知
 */
// 用户登录时获取的初始通知
export function getInitNotification(notifications, messages) {
  return {
    type: GET_INIT_NOTIFICATION,
    payload: {
      notifications: notifications,
      messages: messages,
    }
  }
}

// 用户访问通知或好友界面时清空对应的通知
// clear_notifications
// clear_messages
export function clearNotification(type) {
  return {
    type: CLEAR_NOTIFICATION,
    payload: {
      type: type
    }
  }
}

// 新增通知时增加对应通知个数
// add_notifications
// add_messages
export function addNotification(type) {
  return {
    type: ADD_NOTIFICATION,
    payload: {
      type: type
    }
  }
}
